import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class CamundaService {

  apiBase = "http://localhost:9004";

  constructor(
    private http: HttpClient
  ) { }

  /**
   * 任务列表
   * */
  task_list(user: String): Observable<[Object]>{
    const url = this.apiBase + "/camunda/task/list/" + user;
    return this.http.get<[Object]>(url);
  }

  /**
   * 发起流程
   * */
  submit_start_form(processDefinitionKey: String, param: any){
    const url = this.apiBase + "/camunda/start/" + processDefinitionKey;
    return this.http
      .post(url, param)
      .subscribe();
  }

  /**
   * 获取任务相关数据
   * */
  task_message(taskId: String){
    const url = this.apiBase + "/camunda/task/processDefinition/" + taskId;
    return this.http
      .get<any>(url);
  }

  /**
   * 人事节点-同意
   * */
  pass_personneMatters(taskId:String, message:String){
    const url = this.apiBase + "/camunda/askForLevel/personnelMattersPass/" + taskId;
    return this.http.post<any>(url, {"message": message});
  }

  /**
   * 人事节点-拒绝
   * */
  reject_personneMatters(taskId:String, message:String){
    const url =  this.apiBase + "/camunda/askForLevel/personnelMattersReject/" + taskId;
    return this.http.post<any>(url, {'message': message});
  }

  /**
   * 开始节点--重新发起
   * */
  reStart_starter(taskId:String, variables:{}){
    const url = this.apiBase + "/camunda/askForLevel/starterReStart/" + taskId;
    return this.http.post<any>(url, variables);
  }

  /**
   * 开始节点--撤销流程
   * */
  revoke_starter(taskId:String){
    const url = this.apiBase + "/camunda/askForLevel/starterRevoke/" + taskId;
    return this.http.post<any>(url, {});
  }

  /**
   * 组长节点-同意
   * */
  pass_groupLeader(taskId:String, message:String){
    const url = this.apiBase + "/camunda/askForLevel/groupLeaderPass/" + taskId;
    return this.http.post<any>(url, {"message": message});
  }

  /**
   * 组长节点-驳回
   * */
  reject_groupLeader(taskId:String, message:String){
    const url =  this.apiBase + "/camunda/askForLevel/groupLeaderReject/" + taskId;
    return this.http.post<any>(url, {'message': message});
  }

  /**
   * 主管节点-同意
   * */
  pass_supervisor(taskId:String, message:String){
    const url = this.apiBase + "/camunda/askForLevel/supervisorPass/" + taskId;
    return this.http.post<any>(url, {"message": message});
  }

  /**
   * 主管节点-驳回
   * */
  reject_supervisor(taskId:String, message:String){
    const url =  this.apiBase + "/camunda/askForLevel/supervisorReject/" + taskId;
    return this.http.post<any>(url, {'message': message});
  }
}
